package com.hk.admin.common.base.service;

import com.hk.admin.common.constant.GlobalConstants;
import com.hk.admin.common.base.dto.message.Message;
import com.hk.admin.common.base.dto.page.PageDTO;
import com.hk.admin.common.util.MessageUtil;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;

/**
 * <br>
 * <b>功能：</b>基本的service<br>
 * <b>作者：</b>Pan.ShiJu<br>
 * <b>日期：</b>2017/3/9 21:44<br>
 */
public class BaseService {

    /**
     * 保存成功
     */
    protected ResponseEntity<Message> saveSuccess() {
        return MessageUtil.success(GlobalConstants.SAVE_SUCCESS);
    }

    /**
     * 删除成功
     */
    protected ResponseEntity<Message> deleteSuccess() {
        return MessageUtil.success(GlobalConstants.DELETE_SUCCESS);
    }

    /**
     * 参数id错误
     */
    protected ResponseEntity<Message> idError() {
        return MessageUtil.error(GlobalConstants.ERROR_PARAMETER_ID);
    }


    /**
     * 返回正确信息
     */
    protected ResponseEntity<Message> success(Object msg) {
        return MessageUtil.success(msg);
    }

    /**
     * 返回参数检查错误信息
     */
    protected ResponseEntity<Message> error(String errorMsg) {
        return MessageUtil.error(errorMsg);
    }

    /**
     * 错误提示信息
     */
    protected ResponseEntity<Message> error(Integer errorCode, String errorMsg) {
        return MessageUtil.error(errorCode, errorMsg);
    }

    /**
     * 获取分页查询对象
     */
    protected Pageable getPageable(PageDTO dto) {
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        return new PageRequest(page, size);
    }

    /**
     * 获取分页查询对象
     */
    protected Pageable getPageableAsc(PageDTO dto, String... sortField) {
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        Sort sort = new Sort(Sort.Direction.ASC, sortField);
        return new PageRequest(page, size, sort);
    }

    /**
     * 获取分页查询对象
     */
    protected Pageable getPageableDesc(PageDTO dto, String... sortField) {
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        Sort sort = new Sort(Sort.Direction.DESC, sortField);
        return new PageRequest(page, size, sort);
    }


}
